package com.runen.infinitemovement.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.runen.infinitemovement.R;
import com.runen.infinitemovement.base.BaseActivity;
import com.runen.infinitemovement.model.api.UserAPI;
import com.runen.infinitemovement.model.base.BaseResponseEntity;
import com.runen.infinitemovement.model.entity.MyBodyDataEntity;
import com.runen.infinitemovement.picker.NumberPicker;
import com.runen.infinitemovement.picker.OptionPicker;
import com.runen.infinitemovement.picker.WheelView;
import com.runen.infinitemovement.tool.CommonUtil;
import com.runen.rxnetlibrary.net.callback.BaseResultCallback;
import com.runen.rxnetlibrary.utils.ToastUtils;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class BodyDataActivity extends BaseActivity {

    @BindView(R.id.top_btn_left)
    Button topBtnLeft;
    @BindView(R.id.top_title)
    TextView topTitle;
    @BindView(R.id.tv_weight)
    TextView tvWeight;
    @BindView(R.id.tv_check_weight)
    TextView tvCheckWeight;
    @BindView(R.id.rl_weight)
    RelativeLayout rlWeight;
    @BindView(R.id.tv_height)
    TextView tvHeight;
    @BindView(R.id.tv_check_height)
    TextView tvCheckHeight;
    @BindView(R.id.rl_height)
    RelativeLayout rlHeight;
    @BindView(R.id.tv_heart_rate)
    TextView tvHeartRate;
    @BindView(R.id.tv_check_heart_rate)
    TextView tvCheckHeartRate;
    @BindView(R.id.rl_heart_rate)
    RelativeLayout rlHeartRate;
    @BindView(R.id.tv_save)
    TextView tvSave;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_body_data;
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        ButterKnife.bind(this);
        topTitle.setText("身体数据");
        initData();
    }

    private void initData() {
        showDialog();
        new UserAPI().getMyBodyData(new BaseResultCallback<MyBodyDataEntity>() {
            @Override
            public void onResponse(MyBodyDataEntity response) {
                dismissDialog();
                if (response.code == 0) {
                    if (response.data != null) {
                        hr = response.data.heartRate;
                        ht = response.data.height;
                        wd = response.data.weight;
                        tvCheckWeight.setText(response.data.weight + "kg");
                        tvCheckHeight.setText(response.data.height + "cm");
                        tvCheckHeartRate.setText(response.data.heartRate + "bpm");
                    }
                }
            }
        });
    }

    @OnClick({R.id.top_btn_left, R.id.rl_weight, R.id.rl_height, R.id.rl_heart_rate, R.id.tv_save})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.top_btn_left:
                finish();
                break;
            case R.id.rl_weight:
                onWeightPicker();
                break;
            case R.id.rl_height:
                onHeightPicker();
                break;
            case R.id.rl_heart_rate:
                onHeartRatePicker();
                break;
            case R.id.tv_save:
                onSave();
                break;
        }
    }

    private void onSave() {
        showDialog();
        JSONObject object = new JSONObject();
        try {
            object.put("heartRate", hr);
            object.put("height", ht);
            object.put("weight", wd);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        new UserAPI().putMyBodyData(object.toString(), new BaseResultCallback<BaseResponseEntity<?>>() {
            @Override
            public void onResponse(BaseResponseEntity<?> response) {

            }

            @Override
            public void onComplete() {
                super.onComplete();
                ToastUtils.showShort("保存成功");
                dismissDialog();
            }
        });
    }

    /**
     * 体重
     *
     * @param
     */
    String wd;

    private void onWeightPicker() {
        NumberPicker picker = new NumberPicker(this);
        picker.setSubmitTextColor(CommonUtil.getColor(R.color.color_FFED3F37));
        picker.setCancelTextColor(CommonUtil.getColor(R.color.color_FFED3F37));
        picker.setTextColor(CommonUtil.getColor(R.color.color_FFED3F37));
        picker.setBackgroundResource(R.drawable.bg_rect_u);
        WheelView.LineConfig config = new WheelView.LineConfig();
        config.setColor(CommonUtil.getColor(R.color.color_FFED3F37));//线颜色
        config.setAlpha(255);//线透明度
        //config.setRatio((float) (1.0 / 8.0));//线比率
        picker.setLineConfig(config);
        picker.setOffset(2);//偏移量
        picker.setRange(30, 200);//数字范围
        picker.setSelectedItem(60);
        picker.setLabel("kg");

        picker.setOnOptionPickListener(new OptionPicker.OnOptionPickListener() {
            @Override
            public void onOptionPicked(int index, String item) {
                //保存体重
                wd = item;
                tvCheckWeight.setText(item + "kg");
            }
        });
        CharSequence c = wd;
        String str = String.valueOf((c == null || c.equals("kg")) ? "60kg" : c);
        picker.setSelectedItem(Integer.parseInt((str.replace("kg", "")).replace(".0", "")));
        picker.show();
    }

    String ht;

    private void onHeightPicker() {
        NumberPicker picker = new NumberPicker(this);
        picker.setSubmitTextColor(CommonUtil.getColor(R.color.color_FFED3F37));
        picker.setCancelTextColor(CommonUtil.getColor(R.color.color_FFED3F37));
        picker.setTextColor(CommonUtil.getColor(R.color.color_FFED3F37));
        picker.setBackgroundResource(R.drawable.bg_rect_u);
        WheelView.LineConfig config = new WheelView.LineConfig();
        config.setColor(CommonUtil.getColor(R.color.color_FFED3F37));//线颜色
        config.setAlpha(255);//线透明度
        //config.setRatio((float) (1.0 / 8.0));//线比率
        picker.setLineConfig(config);
        picker.setOffset(2);//偏移量
        picker.setRange(90, 260);//数字范围
        picker.setSelectedItem(170);
        picker.setLabel("cm");
        picker.setOnOptionPickListener(new OptionPicker.OnOptionPickListener() {
            @Override
            public void onOptionPicked(int index, String item) {
                ht = item;
                tvCheckHeight.setText(item + "cm");
                //保存身高
                //  meDetailsPresenter.handleSaveUserInfo("height", item);
            }

        });
        CharSequence c = ht;
        String str = String.valueOf((c == null || c.equals("cm")) ? "170cm" : c);
        picker.setSelectedItem(Integer.parseInt((str.replace("cm", "")).replace(".0", "")));
        picker.show();
    }

    String hr;

    private void onHeartRatePicker() {
        NumberPicker picker = new NumberPicker(this);
        picker.setSubmitTextColor(CommonUtil.getColor(R.color.color_FFED3F37));
        picker.setCancelTextColor(CommonUtil.getColor(R.color.color_FFED3F37));
        picker.setTextColor(CommonUtil.getColor(R.color.color_FFED3F37));
        picker.setBackgroundResource(R.drawable.bg_rect_u);
        WheelView.LineConfig config = new WheelView.LineConfig();
        config.setColor(CommonUtil.getColor(R.color.color_FFED3F37));//线颜色
        config.setAlpha(255);//线透明度
        //config.setRatio((float) (1.0 / 8.0));//线比率
        picker.setLineConfig(config);
        picker.setOffset(2);//偏移量
        picker.setRange(30, 120);//数字范围
        picker.setSelectedItem(70);
        picker.setLabel("bpm");
        picker.setOnOptionPickListener(new OptionPicker.OnOptionPickListener() {
            @Override
            public void onOptionPicked(int index, String item) {
                hr = item;
                tvCheckHeartRate.setText(item + "bpm");
                //保存身高
                //  meDetailsPresenter.handleSaveUserInfo("height", item);
            }

        });
        CharSequence c = hr;
        String str = String.valueOf((c == null || c.equals("bpm")) ? "70bpm" : c);
        picker.setSelectedItem(Integer.parseInt((str.replace("bpm", "")).replace(".0", "")));
        picker.show();
    }
}
